using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using NCT.Framework;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserFamilyProfileBC : IUserFamilyProfile, IUserFamilyProfileAdmin {

        #region IUserFamilyProfile Members

        public bool Insert(UserFamilyProfileEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateCreated = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Insert UserFamilyProfileEnt to DB
                result = UserFamilyProfileDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Insert UserFamilyProfileEnt into Cache
                if (ConfigInfo.UseCache && result) {

                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFamilyProfileBC.Insert({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Update(UserFamilyProfileEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateModified = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Update to DB
                result = UserFamilyProfileDA.Update(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Update User into Cache
                if (ConfigInfo.UseCache && result) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFamilyProfileBC.Update({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public UserFamilyProfileEnt GetByUserKey(string userKey) {
            UserFamilyProfileEnt item = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                if (ConfigInfo.UseCache) {
                }
                else {
                    item = UserFamilyProfileDA.GetByUserKey(userKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFamilyProfileBC.GetByUserKey({0}): \n" + ex.ToString(), userKey));
            }
            return item;
        }

        #endregion
    }
}